package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.WmsRecordOutDTO;
import com.hxzy.entity.WmsCustomer;
import com.hxzy.entity.WmsGoods;
import com.hxzy.entity.WmsRecordOut;
import com.hxzy.entity.WmsRespository;
import com.hxzy.mapper.WmsCustomerMapper;
import com.hxzy.mapper.WmsGoodsMapper;
import com.hxzy.mapper.WmsRecordOutMapper;
import com.hxzy.mapper.WmsRespositoryMapper;
import com.hxzy.service.WmsRecordOutService;
import com.hxzy.util.MybatisUtil;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.WmsRecordOutVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class WmsRecordOutServiceImpl implements WmsRecordOutService {
    @Override
    public BootstrapTableVO search(WmsRecordOutDTO dto) {
        BootstrapTableVO tableVO = new BootstrapTableVO();
        try(SqlSession session = MybatisUtil.getSqlSessionFactory().openSession()){
            //出库
            WmsRecordOutMapper outMapper = session.getMapper(WmsRecordOutMapper.class);
            //客户
            WmsCustomerMapper customerMapper = session.getMapper(WmsCustomerMapper.class);
            //商品
            WmsGoodsMapper goodsMapper = session.getMapper(WmsGoodsMapper.class);
            //仓库
            WmsRespositoryMapper respositoryMapper = session.getMapper(WmsRespositoryMapper.class);

            PageHelper.startPage(dto.getPageNumber(),dto.getPageSize());
            List<WmsRecordOut> list = outMapper.search(dto);

            PageInfo<WmsRecordOut> pg = new PageInfo<>(list);
            tableVO.setTotal(pg.getTotal());
            List<WmsRecordOutVO> arr = new ArrayList<>();
            for (WmsRecordOut out : list) {
                WmsRecordOutVO wmsRecordOutVO = new WmsRecordOutVO();
                BeanUtils.copyProperties(wmsRecordOutVO,out);

                WmsCustomer wmsCustomer = customerMapper.selectByPrimaryKey(out.getRecordCustomerid());
                WmsGoods goods = goodsMapper.selectByPrimaryKey(out.getRecordGoodid());
                WmsRespository wmsRespository = respositoryMapper.selectByPrimaryKey(out.getRecordRepositoryid());

                wmsRecordOutVO.setCustomerName(wmsCustomer.getCustomerName());
                wmsRecordOutVO.setGoodName(goods.getGoodName());
                wmsRecordOutVO.setRepoAddress(wmsRespository.getRepoAddress());

                arr.add(wmsRecordOutVO);
            }
            tableVO.setRows(arr);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return tableVO;
    }

    @Override
    public boolean insert(WmsRecordOut record) {
        int count = 0;
        try(SqlSession session = MybatisUtil.getSqlSessionFactory().openSession()){
            WmsRecordOutMapper mapper = session.getMapper(WmsRecordOutMapper.class);
            count = mapper.insert(record);
            session.commit();
        }
        return count>0;
    }
}
